﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace SharedGenomics.Core
{
    public class WCFProxyFactory
    {
        private IDictionary<Type, Type> _concreteProxyTypeMap;

        public WCFProxyFactory()
        {
            this._concreteProxyTypeMap = new Dictionary<Type, Type>();
        }

        public virtual ClientBase<T> GetProxy<T>() where T : class
        {
            Type interfaceType = typeof(T);
            ExceptionHelper.ThrowIf<ArgumentException>(!this._concreteProxyTypeMap.ContainsKey(interfaceType), "Client proxy type not registered with factory");

            Type proxyType = this._concreteProxyTypeMap[interfaceType];
            ClientBase<T> proxy = (ClientBase<T>)Activator.CreateInstance(proxyType);

            return proxy;
        }

        public void RegisterProxyType<TInterface, TProxy>() where TProxy : ClientBase<TInterface>, new()
                                                            where TInterface : class
        {
            this._concreteProxyTypeMap[typeof(TInterface)] = typeof(TProxy);
        }
    }
}
